#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "bplustree.h"
#include "time.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    btnmove=new QTimer();


    tre=new Bplustree(MAX,this);

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::paintEvent(QPaintEvent *){

    QPainter p;
    p.begin(this);

    while(tre->parent!=NULL)
        tre=tre->parent;
    tre->traversed(tre,&p);
    p.end();


}

void MainWindow::on_pushButton_clicked()
{
    int q;

    for(q=20;q>10;q--){

        while(tre->parent!=NULL)
            tre=tre->parent;
        QString* qw=new QString(ui->lineEdit_2->text());
        if ((tre->find(q))!=NULL){
            qDebug()<<"dublicat";
            return;
        }

        tre->pushkeys(q,(Bplustree*)qw);
    }

    for(q=0;q<10;q++){

        while(tre->parent!=NULL)
            tre=tre->parent;
        QString* qw=new QString(ui->lineEdit_2->text());
        if ((tre->find(q))!=NULL){
            qDebug()<<"dublicat";
            return;
        }

        tre->pushkeys(q,(Bplustree*)qw);
    }


}



void MainWindow::on_pushButton_2_clicked()
{
    int q;
    bool ok;
    q=ui->lineEdit->text().toInt(&ok);
    if (ok&&q)
    {
        while(tre->parent!=NULL)
            tre=tre->parent;
        QString* qw=new QString(ui->lineEdit_2->text());
        if ((tre->find(q))!=NULL){
            qDebug()<<"dublicat";
            return;
        }
        tre->pushkeys(q,(Bplustree*)qw);

    }else{
        qDebug()<<"eror";
    }

    ui->lineEdit_2->setText(" ");
}

void MainWindow::on_pushButton_5_clicked()
{
    ui->centralWidget->repaint();
}

void MainWindow::on_pushButton_4_clicked()
{
    int q;
    bool ok;
    q=ui->lineEdit->text().toInt(&ok);
    if (ok&&q)
    {

        while(tre->parent!=NULL)
            tre=tre->parent;

        ui->lineEdit_2->setText(*((QString*)(tre->find(q))));

    }else{
        qDebug()<<"eror";
    }
    ui->centralWidget->repaint();
}



void MainWindow::on_pushButton_7_clicked()
{


}





void MainWindow::on_pushButton_3_clicked()
{
    //delete
    qDebug()<<"del";
    int q;
    bool ok;
    q=ui->lineEdit->text().toInt(&ok);
    if (ok)
    {
        while(tre->parent!=NULL)
            tre=tre->parent;
        QString* qw=new QString(ui->lineEdit_2->text());
        if ((tre->find(q))==NULL){
            qDebug()<<"elem not found";
            return;
        }
    tre=tre->delelem(q);

    }else{
        qDebug()<<"eror";
    }

    ui->lineEdit_2->setText(" ");
        ui->lineEdit->setText(" ");
}

void MainWindow::on_pushButton_6_clicked()
{
    int q;


    for(q=0;q<10;q++){

        while(tre->parent!=NULL)
            tre=tre->parent;
        QString* qw=new QString(ui->lineEdit_2->text());
        if ((tre->find(q))!=NULL){
            qDebug()<<"dublicat";
            return;
        }

        tre->pushkeys(q,(Bplustree*)qw);
    }


    for(q=20;q>10;q--){

        while(tre->parent!=NULL)
            tre=tre->parent;
        QString* qw=new QString(ui->lineEdit_2->text());
        if ((tre->find(q))!=NULL){
            qDebug()<<"dublicat";
            return;
        }

        tre->pushkeys(q,(Bplustree*)qw);
    }


}
